﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace Bluebunnie
{
    public class MenuOrganizer
    {
        public void Tesselate(Menu menu, List<Lazy<UIElement, IModule>> modules)
        {
            List<MenuItem> items = new List<MenuItem>();

            foreach (Lazy<UIElement, IModule> mod in modules)
            {
                var target = items.FirstOrDefault(x => (string)x.Header == mod.Metadata.Display);
                if (target == null)
                {
                    MenuItem parent = new MenuItem() { Header = mod.Metadata.Parent };
                    items.Add(parent);
                    target = parent;
                }
                else
                    target.Items.Add(new MenuItem { Header = mod.Metadata.Display });                
            }

        }
    }
}
